package com.hex.liyue.usermodule.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.SexUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.activity.HarvestDetailsActivity;
import com.hex.liyue.giftmodule.bean.GiveGiftsBean;
import com.hex.liyue.lookingdatemodule.bean.AppointmentDetailsResultBean;
import com.hex.liyue.usermodule.activity.VideoActivity;
import com.hex.liyue.usermodule.bean.VideoListsBean;

import java.util.List;

import butterknife.Bind;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterVideoLists extends RecyclerView.Adapter<AdapterVideoLists.MyHolder> {
    private OnItemClickListener onItemClickListener;
    private List<VideoListsBean.DataBean> list;
    private MediaController mediaController;
    private Context context;

    public void setList(List<VideoListsBean.DataBean> list) {
        this.list = list;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.adapter_video_lists, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {

//        Glide.with(LiYueApplication.getInstance()).load(list.launch_user.avatar).centerCrop().error(R.drawable.default_error_image).into(holder.ivAvater);
//        ivAvater.set(appointmentDetailsResultBean.data.launch_user.avatar);
//        tvToobarRight.setText("关于" + SexUtils.getXSex(appointmentDetailsResultBean.data.launch_user.sex));
//        holder.tvLocation.setText(list.get(position).event);
//        holder.tvAddress.setText(list.get(position).location);
//        if (TextUtils.isEmpty(list.get(position).remark)) {
//            holder.tvRemark.setText("这个人很懒,什么都没有写");
//
//        } else {
//            holder.tvRemark.setText(list.get(position).remark);
//
//        }
//        long time =list.get(position).invitations_time * 1000;
//        holder.tvBeginDate.setText(DateUtils.getFormatDate(time, "yyyy年MM月dd日 HH:mm"));
        if (TextUtils.isEmpty(list.get(position).video)) {
            holder.ivStartVideo.setVisibility(View.GONE);
        } else {
            holder.ivStartVideo.setVisibility(View.VISIBLE);
            holder.vvVideo.setVideoURI(Uri.parse(list.get(position).video));
        }
        mediaController = new MediaController(context);
        //VideoView与MediaController进行关联
        holder.vvVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                L.d("WJ", "onCompletion");
                holder.rl_start_box.setVisibility(View.VISIBLE);
                Glide.with(LiYueApplication.getInstance())
                        .load(UserManager.getUserAvator()).centerCrop()
                        .error(R.drawable.default_error_image)
                        .into(holder.ivAvater);
                holder.vvVideo.setVisibility(View.GONE);
            }
        });
        holder.vvVideo.setMediaController(mediaController);
        mediaController.setMediaPlayer(holder.vvVideo);
        if (TextUtils.isEmpty(list.get(position).video)) {
            holder.ivStartVideo.setVisibility(View.GONE);
            holder.ivAvater.setVisibility(View.GONE);
        } else {
            holder.ivStartVideo.setVisibility(View.VISIBLE);
            holder.vvVideo.setVideoURI(Uri.parse(list.get(position).video));
        }
        holder.ivStartVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //播放视频
                holder.rl_start_box.setVisibility(View.GONE);
                holder.ivAvater.setVisibility(View.GONE);
                holder.vvVideo.setVisibility(View.VISIBLE);
                //让VideiView获取焦点
                holder.vvVideo.requestFocus();
                holder.vvVideo.start();
            }
        });
        holder.tv_event.setText(list.get(position).event);
        Glide.with(LiYueApplication.getInstance())
                .load(UserManager.getUserAvator())
                .error(R.drawable.default_error_image)
                .transform(new GlideCircleTransform(context))
                .into(holder.ivAvater);
        long time = list.get(position).invitations_time * 1000;
        holder.tv_begin_date.setText(DateUtils.getFormatDate(time, "yyyy年MM月dd日 HH:mm"));
//        holder.rl_start_box.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, VideoActivity.class);
//                intent.putExtra("video",list.get(position).video);
//                context.startActivity(intent);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView ivAvater;
        ImageView ivStartVideo;
        RelativeLayout rl_start_box;
        TextView tv_event;
        TextView tv_begin_date;
        VideoView vvVideo;

        public MyHolder(View itemView) {
            super(itemView);

            tv_event = (TextView) itemView.findViewById(R.id.tv_event);
            tv_begin_date = (TextView) itemView.findViewById(R.id.tv_begin_date);
            ivAvater = (ImageView) itemView.findViewById(R.id.iv_avater);
            ivStartVideo = (ImageView) itemView.findViewById(R.id.iv_start_video);
            rl_start_box = (RelativeLayout) itemView.findViewById(R.id.rl_start_box);
            vvVideo = (VideoView) itemView.findViewById(R.id.vv_video);
        }
    }
}
